package com.smartwebee.android.blespp.hospital;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.google.gson.Gson;
import com.smartwebee.android.blespp.R;
import com.smartwebee.android.blespp.bean.MsgBean;
import com.smartwebee.android.blespp.databinding.ActivityMsgDetailBinding;
import com.smartwebee.android.blespp.http.HttpRequest;
import com.smartwebee.android.blespp.http.RequestInterface;
import com.smartwebee.android.blespp.http.VolleyReqQueue;
import com.smartwebee.android.blespp.resp.BaseResp;
import com.smartwebee.android.blespp.resp.MsgResp;
import com.smartwebee.android.blespp.utils.UIUtils;
import com.smartwebee.android.blespp.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MsgDetailActivity extends BaseActivity implements RequestInterface {

    private ActivityMsgDetailBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_msg_detail);
        TAG = "MsgDetailActivity";
        requestQueue = VolleyReqQueue.getInstance(this).getRequestQueue();
        initView();
    }

    private void httpDeleteMsg() {
        showLoading();
        HttpRequest httpRequest = new HttpRequest(Request.Method.POST, Utils.deleteMessageById,
                this) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("sn", Utils.registerResp.getData().getSn());
                map.put("id", getIntent().getStringExtra("id"));
                return map;
            }
        };
        requestQueue.add(httpRequest);
    }

    private void initView() {
        binding.tvTitle.setText(getIntent().getStringExtra("title"));
        binding.tvContent.setText(getIntent().getStringExtra("content"));
        binding.btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isTaskRoot()) {
                    startActivity(new Intent(MsgDetailActivity.this, SplashActivity.class));
                }
                finish();
            }
        });
        binding.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UIUtils.showTips(MsgDetailActivity.this, "提示", "确定要删除吗", new UIUtils.DialogClickEvent() {
                    @Override
                    public void click(int position) {
                        httpDeleteMsg();
                    }
                });
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (isTaskRoot()) {
            startActivity(new Intent(MsgDetailActivity.this, SplashActivity.class));
        }
        return super.onKeyDown(keyCode, event);

    }

    @Override
    public void onSuccess(String id, String resp) {
        dismissLoading();
        BaseResp baseResp = new Gson().fromJson(resp, BaseResp.class);
        if (Utils.parseData(baseResp)) {
            UIUtils.showToast(this, "删除成功");
            finish();
        }
    }

    @Override
    public void onError(String error) {
        dismissLoading();
        UIUtils.showTips(this, error);
    }
}
